package com.speaktoitcookbook.dao.proxy;

import java.sql.Connection;
import java.util.List;

import com.speaktoitcookbook.bean.Cookbook;
import com.speaktoitcookbook.bean.CookbookDetail;
import com.speaktoitcookbook.dao.ICookbookDao;
import com.speaktoitcookbook.dao.impl.CookbookDaoImpl;
import com.speaktoitcookbook.db.Database;

/**
 * 菜谱数据操作代理类
 * 
 * @author 赛艳志 杨强
 * 
 */
public class CookbookDaoProxy implements ICookbookDao {
    private Connection conn;
    private ICookbookDao dao;

    public CookbookDaoProxy() {
        this.conn = Database.getInstance().getConnection();
        this.dao = new CookbookDaoImpl(conn);
    }

    @Override
    public boolean doCreate(CookbookDetail cookbookDetail) {
        boolean result = false;
        if (null != cookbookDetail) {
            if ((null != cookbookDetail.getCookbook())
                    && (null != cookbookDetail.getMaterials() && (null != cookbookDetail
                            .getSteps()))) {
                result = dao.doCreate(cookbookDetail);
            }
        }
        Database.close(conn);
        return result;
    }

    @Override
    public boolean doCreate(Cookbook cookbook) {
        boolean result = false;
        if (null != cookbook) {
            result = dao.doCreate(cookbook);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public boolean doDelete(Cookbook cookbook) {
        boolean result = false;
        if (null != cookbook) {
            result = dao.doDelete(cookbook);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public boolean doDelete(long cookbookID) {
        boolean result = false;
        if (cookbookID > 0) {
            result = dao.doDelete(cookbookID);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public boolean doUpdate(Cookbook cookbook) {
        boolean result = false;
        if (null != cookbook) {
            result = dao.doUpdate(cookbook);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public boolean doHit(long cookbookID) {
        boolean result = false;
        if (cookbookID > 0) {
            result = dao.doHit(cookbookID);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public boolean doShare(long cookbookID) {
        boolean result = false;
        if (cookbookID > 0) {
            result = dao.doShare(cookbookID);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public List<Cookbook> getCookbook(int type, long start) {
        List<Cookbook> result = null;
        if ((type >= ICookbookDao.SORT_UPDATE)
                && (type <= ICookbookDao.SORT_COLLECT) && (start >= 0)) {
            result = dao.getCookbook(type, start);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public List<Cookbook> getCookbook(String dishName, long start) {
        List<Cookbook> result = null;
        if ((start >= 0) && (null != dishName)) {
            result = dao.getCookbook(dishName, start);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public List<Cookbook> getCookbook(Object... params) {
        List<Cookbook> result = null;
        if (null != params) {
            result = dao.getCookbook(params);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public CookbookDetail getCookbookDetail(long cookbookID) {
        CookbookDetail result = null;
        if (cookbookID > 0) {
            result = dao.getCookbookDetail(cookbookID);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public CookbookDetail getCookbookDetail(Cookbook cookbook) {
        CookbookDetail result = null;
        if (null != cookbook) {
            result = dao.getCookbookDetail(cookbook);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public Cookbook getCookbookInfo(long cookbookID) {
        Cookbook result = null;
        if (cookbookID >= 0) {
            result = dao.getCookbookInfo(cookbookID);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public List<CookbookDetail> getCookbookDetail(Object... params) {
        List<CookbookDetail> result = null;
        if (null != params) {
            result = dao.getCookbookDetail(params);
        }
        Database.close(conn);
        return result;
    }

    @Override
    public long getCookbookCount(Object... params) {
        long result = 0;
        if (null != params) {
            result = dao.getCookbookCount(params);
        }
        Database.close(conn);
        return result;
    }
}
